package kmitl.comsci.bustacker;

import java.util.Timer;
import java.util.TimerTask;

import kmitl.comsci.bustacker.util.ConnectionDetector;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class DisplayActivity extends Activity {
	
	private Boolean isInternetPresent = false;
	private final int time = 2800;
	final Context _context = this;
	private ImageView screen = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		this.displaySplashScreen();
	}
	
	private void displaySplashScreen() {
		screen = (ImageView) findViewById(R.id.splashImage);
		screen.setImageDrawable(getResources().getDrawable(R.drawable.logo_itone));
		
		TimerTask genesis_logo = new TimerTask() {
			@Override
			public void run() {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						screen.setVisibility(View.GONE);
						screen.setImageDrawable(getResources().getDrawable(R.drawable.logo_kmitl));
						screen.setVisibility(View.VISIBLE);
						splashTime.schedule(app_logo, time);
					}
				});

			}

			Timer splashTime = new Timer();
			TimerTask app_logo = new TimerTask() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							screen.setVisibility(View.GONE);
							screen.setImageDrawable(getResources().getDrawable(R.drawable.logo_bustacker));
							screen.setVisibility(View.VISIBLE);
						}
					});

				}
			};
		};
		
		TimerTask toHome = new TimerTask() {
			@Override
			public void run() {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
											
						ConnectionDetector cd = new ConnectionDetector(_context);
						isInternetPresent = cd.isConnectingToInternet();
						
						if (isInternetPresent) {
							finish();
							startActivity(new Intent(DisplayActivity.this, HomeActivity.class));
						} else {
							finish();
							AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(_context);
							alertDialogBuilder.setTitle("No Internet Connection");
							alertDialogBuilder.setMessage("You don't have internet connection.")
									.setCancelable(false)
									.setPositiveButton("Yes",
											new DialogInterface.OnClickListener() {
												public void onClick(DialogInterface dialog, int id) {
													startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
												}
											})
									.setNegativeButton(
											"No",
											new DialogInterface.OnClickListener() {
												public void onClick(DialogInterface dialog,	int id) {
													DisplayActivity.this.finish();
												}
											});
							
							AlertDialog alertDialog = alertDialogBuilder.create();
							alertDialog.show();
						}
						
					}
				});
				
			}
		};

		Timer splashTime = new Timer();
		Timer toHomeTime = new Timer();

		try {
			splashTime.schedule(genesis_logo, time);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			toHomeTime.schedule(toHome, time + time + time);
		}
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
        }
        return true;
	}
}
